<{include file="Public/top.html" }>
</head>
<body>
<div id="warpper">
    <div class="content">
        <div class="location">签到设置</div>
        <div class="ui-tab">
            <{include file="Sign/setNav.html"}>
        </div>

        <!--表格内容-->
        <form id="allInfo">
            <div class="mainGrid">
                <table class="ui-table ui-body">
                    <thead>
                    <tr>
                        <th class="pct9">序号</th>
                        <th class="pct15">奖品名称</th>
                        <th class="pct8">中奖概率</th>
                        <th class="pct8">日中奖限制</th>
                        <th class="pct8">总中奖限制</th>
                        <th class="pct10">奖品类型</th>
                        <th class="pct8">奖品设置</th>
                        <th class="pct8">剩余奖品数量
                            <br/>(不限量填-1)</th>
                        <th class="pct15 last-col">奖品图片</th>
                    </tr>
                    </thead>
                    <tbody>
                    <{for $i=0;$i<$num;$i++}>
                        <tr>
                            <td>
                                <input type="hidden" value="<{$obj[$i].id}>" name="prizeId[]" />
                                <select name="prizeRank[]" class="ui-select" style="width:90%;" onchange="changePrize(this,'<{$i}>')">
                                    <option value="1" <{if $obj[$i].prizeRank==1}>selected<{/if}>>一等奖</option>
                                    <option value="2" <{if $obj[$i].prizeRank==2}>selected<{/if}>>二等奖</option>
                                    <option value="3" <{if $obj[$i].prizeRank==3}>selected<{/if}>>三等奖</option>
                                    <option value="4" <{if $obj[$i].prizeRank==4}>selected<{/if}>>四等奖</option>
                                    <option value="5" <{if $obj[$i].prizeRank==5}>selected<{/if}>>五等奖</option>
                                    <option value="0" <{if $obj[$i].prizeRank==0}>selected<{/if}>>谢谢参与</option>
                                </select>
                            </td>
                            <td>
                               <textarea id="prizeName<{$i}>" name="prizeName[]" class="ui-textarea" style="width:90%; height:80px;"><{$obj[$i].prizeName}></textarea>
                            </td>
                            <td>
                                <input name="hitRate[]" type="text" class="ui-text" style="width:90%;" value="<{$obj[$i].hitRate}>" id="hitRate<{$i}>" required/>
                            </td>
                            <td>
                                <select name="dayLimit[]" class="ui-select" id="dayLimit<{$i}>" style="width:90%;">
                                    <{for $dl=1;$dl<=5;$dl++}>
                                    <option value="<{$dl}>" <{if $obj[$i].dayLimitTimes == $dl}> selected <{/if}>><{$dl}></option>
                                    <{/for}>
                                    <option value="0" <{if $obj[$i].dayLimitTimes == 0}>selected<{/if}>>不限</option>
                                </select>
                            </td>
                            <td>
                                <select name="allLimit[]" class="ui-select" id="allLimit<{$i}>" style="width:90%;">
                                    <{for $al=1;$al<=10;$al++}>
                                    <option value="<{$al}>"  <{if $obj[$i].allLimitTimes == $al}> selected <{/if}>><{$al}></option>
                                    <{/for}>
                                    <option value="0"  <{if $obj[$i].allLimitTimes == 0}> selected <{/if}>>不限</option>
                                </select>
                            </td>
                            <td>
                                <select name="prizeType[]" class="ui-select" id="prizeType<{$i}>" style="width:90%;" onchange="changeType(this,'<{$i}>')">
                                    <{html_options options=$type selected=$obj[$i].prizeType}>
                                </select>
                            </td>
                            <td class="ta_l">
                                <!--输入框-->
                                <input type="text" name="prizeSet[]" class="ui-text" style="width:90%;display: table-cell" id="set<{$i}>" value="<{$obj[$i].extra}>"/>
                                <!--异业券-->
                                <div class="handler" id="elc<{$i}>" style="display:none;">
                                    <a href="javascript:void(0);" class="NewEcoupon" id="elcCard<{$i}>" onclick="eCouponList('<{$i}>','<{$obj[$i].extra}>')"><{if $obj[$i].extra}><{$obj[$i].title}><{else}>导入异业券<{/if}></a>
                                    <input type="text" value="<{$obj[$i].extra}>" id="elcCardId<{$i}>" name="prizeSet[]" style="display: none;" disabled="disabled">
                                </div>
                                <!--卡券-->
                                <select name="prizeSet[]" class="ui-select" style="width:90%;display: none;" id="card<{$i}>" disabled="disabled" onchange="getCardPrizeNum(this,'<{$i}>')">
                                    <{html_options values=$cardID output=$cardTitle selected=$obj[$i].extra}>
                                </select>

                            </td>
                            <td>
                                <input name="prizeNum[]" type="text" class="ui-text" style="width:90%;" id="prizeNum<{$i}>" value="<{$obj[$i].prizeNum}>"/>
                            </td>
                            <td>
                                <span class="picture_box picture_80">
                                    <input id="prizePic<{$i}>" type="hidden" name="prizePic[]" value="<{$obj[$i].prizePic}>">
                                    <div class="a-imgBox" id="imgBox<{$i}>" <{if $obj[$i].prizePic }> style="display:block;" <{else}> style="display:none;" <{/if}>>
                                        <a href="javascript:;" class="a-close" onclick="removeElement('imgBox<{$i}>','prizePic<{$i}>')"></a>
                                        <img name="listImg[]" src="<{$obj[$i].prizePic}>" width="80" height="80" id="listImg<{$i}>"/>
                                    </div>
                                    <div>
                                        <a href="javascript:void(0);" class="a-upload">
                                            <input type="file" name="file" id="addListPic<{$i}>" onclick="upload('<{$i}>')">
                                        </a>
                                        <i>上传图片</i>
                                        <i>80*80</i>
                                    </div>
                                </span>
                            </td>
                        </tr>
                    <{/for}>
                    <tr>
                        <td>
                            <input type="hidden" value="<{$obj[$i].id}>" name="prizeId[]" />
                            <input value="谢谢参与" class="ui-select" style="width:90%;background-color: rgb(217, 217, 217)" readonly/>
                            <input type="hidden" name="prizeRank[]" value="0" class="ui-select"/>
                        </td>
                        <td>
                            <textarea name="prizeName[]" class="ui-textarea" style="width:90%; height:80px;background-color: rgb(217, 217, 217)" readonly>谢谢参与</textarea>
                        </td>
                        <td>
                            <input name="hitRate[]" type="text" class="ui-text" style="width:90%;" value="<{$obj[5].hitRate}>" required/>
                        </td>
                        <td>
                            <input name="dayLimit[]" value="0" class="ui-select" style="display: none;"/>
                        </td>
                        <td>
                            <input name="allLimit[]" value="0" class="ui-select" style="display: none;"/>
                        </td>
                        <td>
                            <input name="prizeType[]" value="0" class="ui-select" style="display: none;"/>
                        </td>
                        <td>
                            <input type="text" name="prizeSet[]" class="ui-text" style="display: none;"/>
                        </td>
                        <td>
                            <input name="prizeNum[]" value="-1" type="text" class="ui-text" style="width:90%;background-color: rgb(217, 217, 217)" readonly/>
                        </td>
                        <td>
                             <span class="picture_box picture_80">
                                 <input id="prizePic" type="hidden" name="prizePic[]" value="<{$obj[5].prizePic}>">
                                <div class="a-imgBox" id="imgBox" <{if $obj[5].prizePic}> style="display:block;" <{else}> style="display:none;" <{/if}>>
                                    <a href="javascript:void(0);" class="a-close" onclick="removeElement('imgBox','prizePic')"></a>
                                    <img name="listImg[]" src="<{$obj[5].prizePic}>" width="80" height="80" id="listImg"/>
                                </div>
                                <div>
                                    <a href="javascript:void(0);" class="a-upload">
                                        <input type="file" name="file" id="addListPic" onclick="upload('')">
                                    </a>
                                    <i>上传图片</i>
                                    <i>80*80</i>
                                </div>
                             </span>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="hidden" value="<{$obj[6].id}>" name="prizeId[]" />
                            <input value="底图上传" class="ui-select" style="width:90%;background-color: rgb(217, 217, 217)" readonly/>
                            <input type="hidden" name="prizeRank[]" value="-1" class="ui-select"/>
                            <input type="hidden" name="prizeName[]" value="底图上传" class="ui-select"/>
                            <input name="hitRate[]" class="ui-text" style="display: none" value="0" />
                            <input name="dayLimit[]" class="ui-text" style="display: none" value="0" />
                            <input name="allLimit[]" class="ui-text" style="display: none" value="0" />
                            <input name="prizeType[]" class="ui-text" style="display: none" value="0" />
                            <input name="prizeSet[]" class="ui-text" style="display: none" value="0" />
                            <input name="prizeNum[]" class="ui-text" style="display: none" value="0" />
                        </td>
                        <td>
                            <span class="picture_box picture_80">
                                <input type="hidden" name="basePic" id="basePic" value="<{$obj[6].prizePic}>">
                                <div class="a-imgBox" id="imgBox6" <{if $obj[6].prizePic}> style="display:block;" <{else}> style="display:none;" <{/if}>>
                                    <a href="javascript:void(0);" class="a-close" onclick="removeElement('imgBox6','basePic')"></a>
                                    <img name="listImg[]" src="<{$obj[6].prizePic}>" width="80" height="80" id="baseImg" />
                                </div>
                                <div>
                                    <a href="javascript:void(0);" class="a-upload">
                                        <input type="file" name="file" id="addBasePic">
                                    </a>
                                    <i>上传图片</i>
                                    <i>680*680</i>
                                </div>
                             </span>
                        </td>
                        <!--<td colspan="6">-->
                            <!--<textarea name="prizeSet[]" class="ui-textarea" style="width:90%; height:80px;" placeholder="请输入活动规则，换行以‘$’结尾。例如：1、每累计签到7天可获得一次抽奖机会。$2、每月满签可额外获得一次抽奖机会。$" required><{$obj[6].extra}></textarea>-->
                        <!--</td>-->
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="integral_btn-group">
                <input type="button" value="保存" class="ui-button" onclick="saveAll()"/>
            </div>
        </form>

    </div>
</div>
<div id="NewEcoupon" style="display:none;">
    <table class="ui-table ui-body">
        <thead>
        <tr>
            <th class="pct20">异业券名称</th>
            <th class="pct10 last-col">导入异业券</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>
                <input type="text" class="ui-text" style="width:90%;" id="newCardTitle" required/>
            </td>
            <td class="ta_l">
                <div class="handler">
                    <a href="javascript:void(0);" class="a-upload-2">
                        <input type="file" name="excel" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" id="uploadThirdCard" value="">点击导入Excel
                        <input type="hidden" value="" id="file">
                    </a>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
</div>
<div id="EcouponList" style="display:none;">
    <table class="ui-table ui-body">
        <thead>
        <tr>
            <th class="pct5">选择</th>
            <th class="pct25">异业券名称</th>
            <th class="pct15">总数量</th>
            <th class="pct15">已领数量</th>
            <th class="pct15 last-col">导入异业券</th>
        </tr>
        </thead>
        <tbody id="electCardList">
            <!--读取异业券-->
        </tbody>
    </table>
    <a href="javascript:void(0);" class="Ecouponlink" id="inputNew">导入新异业券</a>
</div>
<{include file="Public/foot.html" }>
</body>
</html>
<script>
    $(function(){
        var dataId = "<{$obj[0].id}>";
        if(dataId == ''){    //数据为空
            $("select[name='prizeRank[]']").val('');
        }else{
            for(var i=0;i<5;i++){
                showPrizeSet($("#prizeType"+i),i);
            }
        }

    });

    function removeElement(id,src)
    {
        document.getElementById(id).style.display="none";
        //删除图片
        $("#"+src).val('');
    }

    function openCouponList(e) {
        var url = "<{U('Card/readElcCard')}>";
        $.getJSON(url,{id:e}, function (data) {
            $("#electCardList").html('').append(data.dataHtml);
        });
    }
    function eCouponList(i,e){
        openCouponList(e);
        var EcouponList = dialog({
            title: '异业券列表',
            width: 820,
            content: $("#EcouponList"),
            onshow: function(){
//                $("#EcouponList form")[0].reset();
            },
            button: [
                {
                    value: '确定',
                    callback: function () {
                        var eleCardId = $("input[name='Ecoupon']:checked").val();
                        var elcCardTitle = $("input[name='Ecoupon']:checked").parent().next().text();
                        if(!eleCardId){
                            layer.msg("请选择您需要的异页券");
                            return false;
                        }
                        $('#elcCard'+i).text(elcCardTitle);
                        $("#elcCardId"+i).val(eleCardId);

                        var prizeNum = parseInt($("input[name='Ecoupon']:checked").parent().next().next().text())-parseInt($("input[name='Ecoupon']:checked").parent().next().next().next().text());
                        $("#prizeNum"+i).val(prizeNum);
                    },
                    autofocus: true
                },
            ]
        });
        EcouponList.showModal();
    }
    $(document).on('click','#inputNew',function(){
        var NewEcoupon = dialog({
            title: '新增异业券',
            width: 500,
            content: $("#NewEcoupon"),
            onshow: function(){
                $("#NewEcoupon form")[0].reset();
            },
            button: [
                {
                    value: '保存',
                    callback: function () {
                        var title = $("#newCardTitle").val();
                        var path = $("#file").val();
                        if(!title){
                            $('#newCardTitle').css("border","2px solid #fe7f30");
                            layer.msg("请填写异业券标题");
                            return false;
                        } else{
                            $('#newCardTitle').css("border","1px solid #e5e5e5");
                        }
                        if(!path){
                            layer.msg("请导入异业券文件");
                            return false;
                        }
                        $.ajax({
                            type : 'get',
                            url : "<{U('Card/saveThirdCardInfo')}>",
                            dataType : 'json',
                            data : {title:title,path:path},
                            success:function(data){
                                layer.msg(data.msg);
                                if(data.id == 0){
                                    //重新打开异业券选择页面
                                    openCouponList(data.data);
                                }
//
                            }
                        });
                    },
                    autofocus: true
                },
            ]
        });
        NewEcoupon.showModal();
    });

    //改变奖品等级
    function changePrize(item,i) {

        if($(item).val()== 0){ //谢谢参与
            $("#prizeName" + i).val("谢谢参与").attr("readonly", true).css('background-color', 'rgb(217, 217, 217)');
            $("#dayLimit" + i + ",#allLimit" + i + ",#prizeType" + i + ",#set" + i).css('display', 'none');
            $("#prizeNum" + i).val(-1).attr("readonly", true).css('background-color', 'rgb(217, 217, 217)');
            $("#prizeType"+i).children("option").eq(0).val(0);
            $("#hitRate"+i).val();
        } else {
            $("#prizeName" + i + ",#prizeNum" + i + ",#set" + i + ",#hitRate" + i).val('').css('background-color', '#fff').attr("readonly", false);
            $("#dayLimit" + i + ",#allLimit" + i + ",#prizeType" + i + ",#set" + i).css('display', 'block');
            $("#prizeType" + i).unbind('change').bind('change', function () {changeType(this, i); }).val(2);
            $("#dayLimit"+i+",#allLimit"+i).val(0);
            $("#elc"+i+",#card"+i).css('display','none');
        }
    }

    //改变奖品类型
    function changeType(item,i){

        if($(item).val() == 6){ //异业券
            $('#elc'+i).css('display','table-cell');
            $('#set'+i+",#card"+i).css('display','none').attr('disabled',true);
            $("#elcCard"+i).text("导入异业券");
            $("#elcCardId"+i).removeAttr('disabled');
            $("#prizeNum"+i).attr('readonly',true).css('background-color','rgb(217, 217, 217)');
        } else if($(item).val() == 4){  //卡券
            $('#card'+i).css('display','table-cell').removeAttr('disabled').val('0');
            $('#elc'+i+',#set'+i).css('display','none');
            $('#elcCardId'+i+',#set'+i).attr('disabled',true);
            $("#prizeNum"+i).attr('readonly',false).css('background-color','#fff');
        } else{
            if($(item).val() == '5'){   //实物
                $("#set"+i).css('display','none');
            } else{
                $('#set'+i).css('display','table-cell').removeAttr('disabled').val('');
            }
            $('#elc'+i+',#card'+i).css('display','none');
            $('#elcCardId'+i+',#card'+i).attr('disabled',true);
            $("#prizeNum"+i).attr('readonly',false).css('background-color','#fff');
        }
    }

    //卡券读取数量
    function getCardPrizeNum(item,i){
        var getCardNumUrl = "<{U('Sign/getCardAvailableNum')}>";
        $.getJSON(getCardNumUrl,{cid:$(item).val()},function(data){
            console.log(data);
            if(data.status !=0 ){
                layer.msg(data.msg);
            } else{
                $("#prizeNum"+i).val(data.msg);
            }
        });
    }

    //有数据后加载显示奖品设置
    function showPrizeSet(item,i){
        if($(item).val() == 6){ //异业券
            $('#elc'+i).css('display','table-cell');
            $('#set'+i+",#card"+i).css('display','none').attr('disabled',true);
            $("#elcCardId"+i).removeAttr('disabled');
            $("#prizeNum"+i).attr('readonly',true).css('background-color','rgb(217, 217, 217)');
        } else if($(item).val() == 4){  //卡券
            $('#card'+i).css('display','table-cell').removeAttr('disabled');
            $('#elc'+i+',#set'+i).css('display','none');
            $('#elcCardId'+i+',#set'+i).attr('disabled',true);
            $("#prizeNum"+i).attr('readonly',false).css('background-color','#fff');
        } else{
            if($(item).val() == '5'){   //实物
                $("#set"+i).css('display','none');
            } else{
                $('#set'+i).css('display','table-cell').removeAttr('disabled');
            }
            $('#elc'+i+',#card'+i).css('display','none');
            $('#elcCardId'+i+',#card'+i).attr('disabled',true);
            $("#prizeNum"+i).attr('readonly',false).css('background-color','#fff');
        }
    }

    //上传图片
    function upload(i){
        var $listPic=$("#addListPic"+i);
        $listPic.AjaxFileUpload({
            action: './api.php?m=Api&c=index&a=ajaxUpload&from=IMG_SIGN_PRIZE',
            onSubmit: function () {
                console.log("开始上传");
            },
            onComplete: function (filename, response) {
                if(parseInt(response.id)>0){
                    console.log("上传成功");
                    layer.msg("上传成功");
                    $("#prizePic"+i).val(response.msg);
                    $("#listImg"+i).attr('src',response.msg);
                    document.getElementById('imgBox'+i).style.display="block";
                }else{
                    console.log(response.msg);
                    layer.msg(response.msg);
                }
            }
        });
    }

    //上传底图
    var addBasePic = $('#addBasePic');
    addBasePic.AjaxFileUpload({
        action: './api.php?m=Api&c=index&a=ajaxUpload&from=IMG_SIGN_TURNTABLE',
        onSubmit: function () {
            console.log("开始上传");
        },
        onComplete: function (filename, response) {
            if(parseInt(response.id)>0){
                layer.msg("上传成功");
                $('#basePic').val(response.msg);
                $("#baseImg").attr('src',response.msg);
                document.getElementById('imgBox6').style.display="block";
            }else{
                console.log(response.msg);
                layer.msg(response.msg);
            }
        }
    });

    //上传异业券
    var thirdCard = $("#uploadThirdCard");
    thirdCard.AjaxFileUpload({
        action: "<{U('card/uploadThirdCard')}>",
        onSubmit: function () {
            console.log("开始上传");
        },
        onComplete: function (filename, response) {
            if(parseInt(response.id)>0){
                console.log("上传成功");
                layer.msg("上传成功");
                $("#file").val(response.msg);
            }else{
                console.log(response.msg);
                layer.msg(response.msg);
            }
        }
    });

    //新增导入异页券
    function addInsert(id){
        var addCard = $("#addInsert" +id);
        addCard.AjaxFileUpload({
            action:"<{U('card/addImportCard')}>&id="+id,
            onSubmit:function(){
                console.log("开始上传");
            },
            onComplete:function(filename,response){
                if(parseInt(response.id)==0){
                    console.log("上传成功");
                    layer.msg("上传成功");
                    //重新打开异业券选择页面
                    openCouponList(id);
                } else{
                    console.log(response.msg);
                    layer.msg(response.msg);
                }
            }
        });
    }

    //删除异页券
    function del(e,id){
        if(confirm("确定删除？")){
            $.getJSON("<{U('Card/del')}>",{id:e},function(data){
                layer.msg(data.msg);
                //重新打开异业券选择页面
                openCouponList(id);
            });
        }
    }

    //提交数据
    function saveAll(){
        var param = $("#allInfo").serialize();
        var url = "<{U('Sign/saveLuckyInfo')}>&type=2";
        var basePic = $("input[name='basePic']").val();
        if(!basePic || basePic==''){
            layer.msg('请更新底图');
            return false;
        }
        $.getJSON(url,param,function(data){
            console.log(data);
            if(data.status>0){
                layer.msg(data.msg);
                if(data.status==2){
                    layer.msg(data.msg);
                    $("#prizeNum"+data.id).css("border","2px solid #fe7f30");
                }
            } else{
                layer.msg(data.msg);
                window.location.reload();
            }
        });
    }
</script>
